﻿using System;
using System.Configuration;
using System.Diagnostics;
using BookingEngine.Data.Fog.Booking;

namespace BookingEngine.Data
{
    public static class HotelBookService
    {
        private static readonly loginRQ s_LoginZh;
        private static readonly loginRQ s_LoginEn;

        static HotelBookService()
        {
            s_LoginZh = new loginRQ
                            {
                                user = ConfigurationManager.AppSettings["FogUserName"],
                                key = ConfigurationManager.AppSettings["FogPassword"],
                                lang = "zh",
                            };
            s_LoginEn = new loginRQ
                            {
                                user = ConfigurationManager.AppSettings["FogUserName"],
                                key = ConfigurationManager.AppSettings["FogPassword"],
                                lang = "en",
                            };
        }

        #region 数据查询

        /// <summary>
        /// </summary>
        /// <param name = "language">语言种类(支持中英日文)，‘zh’,’en’,’jp’</param>
        /// <param name = "hotelCode">FOG提供的酒店ID</param>
        /// <param name = "checkInDate">入住日期</param>
        /// <param name = "checkOutDate">离店日期</param>
        /// <param name = "ratePlanCode">价格计划代码</param>
        /// <param name = "roomsNumber">预订房间数</param>
        /// <param name = "adultsNumber">入住人数</param>
        /// <param name = "holdTimeHour">保留时间(小时)</param>
        /// <param name = "holdTimeMinute">保留时间(分钟)</param>
        /// <returns></returns>
        public static bookingRateRulesRS GetBookingRate(string language, string hotelCode, DateTime checkInDate, DateTime checkOutDate, string ratePlanCode, int roomsNumber, int adultsNumber, int holdTimeHour, int holdTimeMinute)
        {
            if (string.IsNullOrEmpty(hotelCode))
            {
                throw new ArgumentNullException("hotelCode");
            }
            try
            {
                using (var serviceClient = new HotelBookingWebServiceClient())
                {
                    var bookingRateRulesRS = serviceClient.getBookingRateAndRules(language == "en" ? s_LoginEn : s_LoginZh, hotelCode, checkInDate.Date.ToString("yyyy-MM-dd"), checkOutDate.Date.ToString("yyyy-MM-dd"), ratePlanCode, roomsNumber, adultsNumber, string.Empty, string.Format("{0}:{1}", holdTimeHour.ToString("00"), holdTimeMinute.ToString("00")));
                    if (bookingRateRulesRS != null && bookingRateRulesRS.result != null && bookingRateRulesRS.result.success)
                    {
                        return bookingRateRulesRS;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return null;
        }

        #endregion
    }
}